package org.jeecg.modules.kuner.collectionbase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.kuner.browsebase.vo.BrowseHisVO;
import org.jeecg.modules.kuner.collectionbase.entity.KrCollectionBase;
import org.jeecg.modules.kuner.collectionbase.mapper.KrCollectionBaseMapper;
import org.jeecg.modules.kuner.collectionbase.service.IKrCollectionBaseService;
import org.jeecg.modules.putil.KrStatic;
import org.jeecg.modules.putil.LoginUserUtil;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;

/**
 * @Description: 收藏记录
 * @Author: jeecg-boot
 * @Date: 2025-02-18
 * @Version: V1.0
 */
@Service
public class KrCollectionBaseServiceImpl extends ServiceImpl<KrCollectionBaseMapper, KrCollectionBase> implements IKrCollectionBaseService {

    @Resource
    private LoginUserUtil loginUserUtil;

    /**
     * 职位收藏
     *
     * @param userId    用户ID
     * @param contentId 内容ID
     * @param type      收藏类型
     */
    @Override
    public void collection(String userId, String contentId, KrStatic.ContentType type) {
        if (getCollectionInfo(userId, contentId, type) != null) {
            throw new JeecgBootException("请勿重复收藏！");
        }
        KrCollectionBase collectionBase = new KrCollectionBase();
        collectionBase.setType(type);
        collectionBase.setUserId(userId);
        collectionBase.setContentId(contentId);
        save(collectionBase);
    }

    /**
     * 职业取消收藏
     *
     * @param userId      用户ID
     * @param contentId   被收藏的正文ID
     * @param contentType 收藏类型
     */
    @Override
    public void unCollection(String userId, String contentId, KrStatic.ContentType contentType) {
        KrCollectionBase collectionInfo = getCollectionInfo(userId, contentId, contentType);
        if (collectionInfo == null) {
            throw new JeecgBootException("找不到对应的收藏信息！");
        }
        removeById(collectionInfo);
    }

    /**
     * 查询收藏信息
     *
     * @param userId      用户ID
     * @param contentId   内容ID
     * @param contentType 内容类型
     * @return 查询结果
     */
    public KrCollectionBase getCollectionInfo(String userId, String contentId, KrStatic.ContentType contentType) {
        return getOne(new LambdaQueryWrapper<KrCollectionBase>().eq(KrCollectionBase::getUserId, userId).eq(KrCollectionBase::getContentId, contentId).eq(KrCollectionBase::getType, contentType));
    }

    @Override
    public IPage<BrowseHisVO> myCollection(Integer pageNo, Integer pageSize, KrStatic.ContentType type) {
        IPage<BrowseHisVO> page = new Page<>(pageNo, pageSize);
        String id = loginUserUtil.getLoginUser().getId();
        if (KrStatic.ContentType.CURRICULUM_VITAE.equals(type)) {
            return this.baseMapper.selectCurrCollection(page, id);
        } else if (KrStatic.ContentType.HIRE.equals(type)) {
            return this.baseMapper.selectHireCollection(page, id);
        }
        throw new JeecgBootException("错误的分类信息！");
    }
}
